package com.awspaas.user.apps.cpq.job;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.rabbitmq.client.AMQP.Confirm.Select;

public class RefreshMaterialJob implements IJob{

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try {
			String sql="select  NAME,BM,CLF,RGF,WXWG,CGF,DJ from BO_EU_CPQ_COMPONENT_PART where TYPE in('制造件','采购件')";
			List<RowMap> maps=DBSql.getMaps(sql);
			Map<String, RowMap> wlmap=new HashMap<String, RowMap>();
			if(maps.size()>0) {
				for (RowMap rowMap : maps) {
					wlmap.put(rowMap.getString("BM"), rowMap);
				}
				
			}
			String allCPsql = "select   a.BM,a.ID,a.LX,a.SL,"	
					+ "a.CLF,"
					+ "a.RGF,a.WXWG,a.CGF,"
					+ "a.DJ,"
					+ "a.ZJ,"
					+ "a.ZCLF,"
					+ "a.ZGSF,a.ZWX,a.ZCGF,"
					+ "b.NAME,b.ZZFBL,b.BM as BH,"
					+ "b.DJ as JG,"
					+ "b.CLF as WCLF,"
					+ "b.RGF as WRGF,b.WXWG as WX,b.CGF as WCGF "
					+ "from  BO_EU_CPQ_COMPONENT_PART_GRID a, BO_EU_CPQ_COMPONENT_PART b "
					+ "where  a.BINDID=b.BINDID and  b.TYPE='装配件' ";
			List<RowMap> cpmaps=DBSql.getMaps(allCPsql);
			Map<String, List<RowMap>> cpmap=new HashMap<String, List<RowMap>>();
			Map<String, List<RowMap>> cp=new HashMap<String, List<RowMap>>();
			if(cpmaps.size()>0) {
				for (RowMap rowMap : cpmaps) {
					String bh=rowMap.getString("BH");
					String LX=rowMap.getString("LX");
					if(cpmap.containsKey(bh)) {
						List<RowMap> lists=cpmap.get(bh);
						lists.add(rowMap);						
						cpmap.put(bh, lists);						
					}else {
						List<RowMap> lists=new ArrayList<RowMap>();						
						lists.add(rowMap);
						cpmap.put(bh, lists);						
					}
					if(cp.containsKey(bh)) {
						if(LX.equals("装配件")) {
							List<RowMap> list=cp.get(bh);
							list.add(rowMap);
							cp.put(bh, list);
						}
					}else {
						if(LX.equals("装配件")) {
							List<RowMap> list=new ArrayList<RowMap>();
							list.add(rowMap);
							cp.put(bh, list);
						}
					}										
				}
				//判断是否存在互相关联的数据
				
				if(cp.size()>0) {
					
					for (String  key : cp.keySet()) {
						List<RowMap> lists=cp.get(key);
						for (RowMap rowMap : lists) {
							String bm=rowMap.getString("BM");
							if(cp.containsKey(bm)) {
								List<RowMap> gridlist=cp.get(bm);
								for (RowMap rowMap2 : gridlist) {
									if(rowMap2.getString("BM").equals(key)) {
										throw new BPMNError("003","编码为："+key+",和编码为："+bm+"互相关联");
									}else {																			
											List<RowMap> grids=cp.get(bm);
											Boolean is=true;
										while (is) {																																		
												for (RowMap rowMap3 : grids) {
													if(rowMap3.getString("BM").equals(key)) {
														throw new BPMNError("003","编码为："+key+",和编码为："+rowMap3.getString("BM")+"互相关联");
													}else {
														if(cp.containsKey(rowMap3.getString("BM"))) {
															grids=cp.get(rowMap3.getString("BM")); 
														}else {
															is=false;
														}																																										
													}
												}
												
											}										
									}
									
								}
							}
						}
					}
				}
				//获取所有装配件中的人工费和外协费用
				String  rgSql="select   a.*,b.BM,b.NAME from  (" + 
						"select  SUM(TOTAL_PRICE) as PRICE,BINDID from  BO_EU_BO_EU_CPQ_COMPONENT_PART_USER_PRICE_GRID  "
						+ "GROUP BY BINDID ) a ,BO_EU_CPQ_COMPONENT_PART b where a.BINDID=b.BINDID and b.TYPE='装配件'";
				String  wxSql="select   a.*,b.BM,b.NAME from  ( " + 
						"select  SUM(TOTAL_PRICE)as PRICE,BINDID from  BO_EU_CPQ_COMPONENT_PART_OUTSOURCING_GRID "
						+ "GROUP BY BINDID)a ,BO_EU_CPQ_COMPONENT_PART b where a.BINDID=b.BINDID and b.TYPE='装配件'";
				List<RowMap> rgmaps=DBSql.getMaps(rgSql);
				List<RowMap> wxmaps=DBSql.getMaps(wxSql);
				Map<String,String> rgmap=new HashMap<String,String>();
				Map<String,String> wxmap=new HashMap<String,String>();
				if(rgmaps.size()>0) {
					for (RowMap rowMap : rgmaps) {
						String bh=rowMap.getString("BM");												
						rgmap.put(bh, rowMap.getString("PRICE"));						
					}
				}	
				if(wxmaps.size()>0) {
					for (RowMap rowMap : wxmaps) {
						String bh=rowMap.getString("BM");												
						wxmap.put(bh, rowMap.getString("PRICE"));						
					}
				}	
				List<String> sqlList=new ArrayList<>();
				Map<String , JSONObject> gxmap=new HashMap<>();
				Map<String , JSONObject> onemap=new HashMap<>();
				for (String bh : cpmap.keySet()) {
					List<RowMap> lists=cpmap.get(bh);
					if(bh.equals("RJC020002")) {
						System.out.println("bh:"+bh+",size="+lists.size());
					}
					if(lists.size()>0) {
						RowMap data=lists.get(0);
						 BigDecimal djnum=new BigDecimal("0");				 			
						 BigDecimal clfnum=new BigDecimal("0");					 
						 BigDecimal rgfnum=new BigDecimal("0");				 
						 BigDecimal wxfnum=new BigDecimal("0");				 
						 BigDecimal cgfnum=new BigDecimal("0");	
						 String isTrue="0"; 
						 String isOne="0"; 
						for (RowMap rowMap : lists) {
							String type=rowMap.getString("LX");
							if(type.equals("装配件")) {
								isOne="1";
							}
							
							String bm=rowMap.getString("BM");
							String id=rowMap.getString("ID");
							String SL=rowMap.getString("SL").isEmpty()?"0":rowMap.getString("SL");//数量	
							BigDecimal sl=new BigDecimal(SL);	
							if(wlmap.containsKey(bm)) {
								
								  isTrue="1"; 
								RowMap wlMap2=	wlmap.get(bm);
								 String newclf=wlMap2.getString("CLF").isEmpty()?"0":wlMap2.getString("CLF");//材料费	
								 String newrgf=wlMap2.getString("RGF").isEmpty()?"0":wlMap2.getString("RGF");//材料费	
								 String newwx=wlMap2.getString("WXWG").isEmpty()?"0":wlMap2.getString("WXWG");//材料费	
								 String newcgf=wlMap2.getString("CGF").isEmpty()?"0":wlMap2.getString("CGF");//材料费	
								 String newdj=wlMap2.getString("DJ").isEmpty()?"0":wlMap2.getString("DJ");//材料费	
								 BigDecimal djNew=new BigDecimal(newdj);							
									BigDecimal clfNew=new BigDecimal(newclf);							
									BigDecimal rgfNew=new BigDecimal(newrgf);
									BigDecimal wxNew=new BigDecimal(newwx);
									BigDecimal cgfNew=new BigDecimal(newcgf);
									//新的物料总价为新的单价x数量									
									BigDecimal	zjNew=djNew.multiply(sl);										
									//新的物料总材料费为新的材料费x数量											
									BigDecimal	zclfNew=sl.multiply(clfNew);										
									//新的物料总人工费为新的人工费x数量											
									BigDecimal	zgsfNew=sl.multiply(rgfNew);
									//新的物料总外协费为新的外协费x数量										
									BigDecimal	zwxNew=sl.multiply(wxNew);
									//新的物料总采购费为新的采购费x数量										
									BigDecimal	zcgfNew=sl.multiply(cgfNew);																		
									 String upSql= "update BO_EU_CPQ_COMPONENT_PART_GRID set DJ= '"+newdj+"',CLF= '"+newclf+"' ,RGF='"+newrgf+"',WXWG='"+newwx+"'"
										 		+ ",CGF='"+newcgf+"',ZJ='"+zjNew+"',ZCLF='"+zclfNew+"',ZGSF='"+zgsfNew+"',ZWX='"+zwxNew+"',ZCGF='"+zcgfNew+"' "
										 				+ "  where  ID='"+id+"' ; ";
											//upSql=String.format(upSql, cldjNew,clfNew,zjNew,zclfNew,ID);
									 if(bh.equals("RJC020002")) {
											System.out.println("bm:"+bm+",,DJ:"+djnum+"ZJ:"+zjNew);
										}
									sqlList.add(upSql);
									
									djnum=djnum.add(zjNew);
									clfnum=clfnum.add(zclfNew);
									rgfnum=rgfnum.add(zgsfNew);
									wxfnum=wxfnum.add(zwxNew);
									cgfnum=cgfnum.add(zcgfNew);
							}
							
						}
						if(isTrue.equals("1")) {
							//判断该物料是否有其他的人工费和外协费
							if(rgmap.containsKey(bh)) {
							String rg=	rgmap.get(bh).isEmpty()?"0":rgmap.get(bh);
							BigDecimal rgs=new BigDecimal(rg);
							rgfnum=rgfnum.add(rgs);
							djnum=djnum.add(rgs);
							}
							if(wxmap.containsKey(bh)) {
								String wx=	wxmap.get(bh).isEmpty()?"0":wxmap.get(bh);
								BigDecimal wxs=new BigDecimal(wx);
								wxfnum=wxfnum.add(wxs);
								djnum=djnum.add(wxs);
								}
							
							
							JSONObject jsonObject=new JSONObject();													
							jsonObject.put("BH", bh);
							jsonObject.put("DJ", djnum);
							jsonObject.put("CLF", clfnum);
							jsonObject.put("RGF", rgfnum);
							jsonObject.put("WXWG", wxfnum);
							jsonObject.put("CGF", cgfnum);
							jsonObject.put("ZZFBL",data.getString("ZZFBL"));
							jsonObject.put("ZJS","");
							gxmap.put(bh, jsonObject);
							if(isOne.equals("0")) {
								onemap.put(bh, jsonObject);
							}
							if(bh.equals("RJC020002")) {
								System.out.println("BH:"+bh+",DJ:"+djnum);
							}	
						}
					}
				}
				//
				JSONObject josn5=gxmap.get("RJC020002");
				System.out.println("json5++++++++++++++++++:"+josn5.toString());
				
				//对装配件进行遍历，
				Boolean is = true;
				List<String> list=new ArrayList<>();
				while (is) {
					int i=list.size();
					
					 for (String key : cpmap.keySet()) {
						 List<RowMap> lists=cpmap.get(key);
						 RowMap data=lists.get(0);
						 String dj="0";//单价					
						 String rgf="0";//人工费
						 String clf="0";//材料费					 
						 String wxf="0";//外协费
						 String cgf="0";//采购费
						 String BH=data.getString("BH");//物料编号
						//判断在已更新装配件map中是否存在
					 		if(gxmap.containsKey(key)) {
					 			JSONObject json=gxmap.get(key);
					 			rgf=json.getString("RGF");
					 			dj=json.getString("DJ");
					 			clf=json.getString("CLF");
					 			wxf=json.getString("WXWG");
					 			cgf=json.getString("CGF");
					 		}
					 		
							
					 		//物料编号不包含勾选的装配件编号数据。
							 BigDecimal djnum=new BigDecimal(dj);				 						
							 BigDecimal clfnum=new BigDecimal(clf);				 						 
							 BigDecimal rgfnum=new BigDecimal(rgf);				 							 
							 BigDecimal wxfnum=new BigDecimal(wxf);				 							
							 BigDecimal cgfnum=new BigDecimal(cgf);	
							 String isTrue="0"; 
							 String bhs="";
							 if(onemap.containsKey(key)) {
						 			JSONObject json1=onemap.get(key);
									 bhs=json1.getString("ZJS");
						 		}
							 for (RowMap rowMap : lists) {
									String BM=rowMap.getString("BM");//编码	
									//获取对应更新的物料信息
									if(onemap.containsKey(BM)) {
										
										if(onemap.containsKey(key)) {

											if(bhs.isEmpty()||bhs.indexOf(BM)<0) {
												
												 isTrue="1";
												 String id=rowMap.getString("ID");//子项ID
													String SL=rowMap.getString("SL").isEmpty()?"0":rowMap.getString("SL");//数量
													 BigDecimal sl=new BigDecimal(SL);					 																																														
													BigDecimal zjNew=new BigDecimal("0");											
													BigDecimal zclfNew=new BigDecimal("0");			
													BigDecimal zgsfNew=new BigDecimal("0");
													BigDecimal zwxNew=new BigDecimal("0");
													BigDecimal zcgfNew=new BigDecimal("0");
													JSONObject json=onemap.get(BM);
													String newdj=json.getString("DJ");													
													BigDecimal djNew=new BigDecimal(newdj);													
													//新的装配件总价=原装配件单价-子件总价+新子件单价x子件数量
													djnum=djnum.subtract(zjNew).add(djNew.multiply(sl));
													zjNew=djNew.multiply(sl);													
													//判断材料费是否相等,不相等，则计算当前装配件的材料费，并计算当前装配件子项的物料的材料费。
													String newclf=json.getString("CLF");
													BigDecimal clfNew=new BigDecimal(newclf);																																						
													zclfNew=sl.multiply(clfNew);
													clfnum=clfnum.add(sl.multiply(clfNew));															
													//判断人工费是否相等,不相等，则计算当前装配件的人工费，并计算当前装配件子项的物料的人工费。
													String newrgf=json.getString("RGF");
													BigDecimal rgfNew=new BigDecimal(newrgf);												
													zgsfNew=sl.multiply(rgfNew);
													rgfnum=rgfnum.add(sl.multiply(rgfNew));													
													//判断外协费是否相等,不相等，则计算当前装配件的外协费，并计算当前装配件子项的物料的外协费。
													String newwx=json.getString("WXWG");
													BigDecimal wxNew=new BigDecimal(newwx);													
													zwxNew=sl.multiply(wxNew);
													wxfnum=wxfnum.add(sl.multiply(wxNew));													
													//判断采购费是否相等,不相等，则计算当前装配件的采购费，并计算当前装配件子项的物料的采购费。
													String newcgf=json.getString("CGF");
													BigDecimal cgfNew=new BigDecimal(newcgf);
													String CGF=rowMap.getString("CGF");//采购费												
													zcgfNew=sl.multiply(cgfNew);
													cgfnum=cgfnum.add(sl.multiply(cgfNew));
													String upSql= "update BO_EU_CPQ_COMPONENT_PART_GRID set DJ= '"+newdj+"',CLF= '"+newclf+"' ,RGF='"+newrgf+"',WXWG='"+newwx+"'"
														 		+ ",CGF='"+newcgf+"',ZJ='"+zjNew+"',ZCLF='"+zclfNew+"',ZGSF='"+zgsfNew+"',ZWX='"+zwxNew+"',ZCGF='"+zcgfNew+"' "
														 				+ " where  ID='"+id+"' ; ";
															//upSql=String.format(upSql, cldjNew,clfNew,zjNew,zclfNew,ID);
															sqlList.add(upSql);
														//将子项的编号存放在bhs中
														if(bhs.isEmpty()) {
															bhs=BM;
														}else {
															bhs=bhs+","+BM;
														}
														if(key.equals("RJC020002")) {
															System.out.println("bm+++++:"+BM+",,DJ:"+djnum+"ZJ:"+zjNew);
															
														}
											}
										}else {
											
											isTrue="1";
											 String id=rowMap.getString("ID");//子项ID
												String SL=rowMap.getString("SL").isEmpty()?"0":rowMap.getString("SL");//数量
												 BigDecimal sl=new BigDecimal(SL);					 																																																											
												BigDecimal zjNew=new BigDecimal("0");										
												BigDecimal zclfNew=new BigDecimal("0");										
												BigDecimal zgsfNew=new BigDecimal("0");
												BigDecimal zwxNew=new BigDecimal("0");
												BigDecimal zcgfNew=new BigDecimal("0");
												JSONObject json=onemap.get(BM);
												String newdj=json.getString("DJ");										
												BigDecimal djNew=new BigDecimal(newdj);										
												//新的装配件总价=原装配件单价-子件总价+新子件单价x子件数量
												djnum=djnum.add(djNew.multiply(sl));
												zjNew=djNew.multiply(sl);										
												//判断材料费是否相等,不相等，则计算当前装配件的材料费，并计算当前装配件子项的物料的材料费。
												String newclf=json.getString("CLF");										
												BigDecimal clfNew=new BigDecimal(newclf);																						
													zclfNew=sl.multiply(clfNew);
													clfnum=clfnum.add(sl.multiply(clfNew));													
												//判断人工费是否相等,不相等，则计算当前装配件的人工费，并计算当前装配件子项的物料的人工费。
												String newrgf=json.getString("RGF");
												BigDecimal rgfNew=new BigDecimal(newrgf);																					
													zgsfNew=sl.multiply(rgfNew);
													rgfnum=rgfnum.add(sl.multiply(rgfNew));											
												//判断外协费是否相等,不相等，则计算当前装配件的外协费，并计算当前装配件子项的物料的外协费。
												String newwx=json.getString("WXWG");
												BigDecimal wxNew=new BigDecimal(newwx);
												String WXWG=rowMap.getString("WXWG");//外协费																	
													zwxNew=sl.multiply(wxNew);
													wxfnum=wxfnum.add(sl.multiply(wxNew));											
												//判断采购费是否相等,不相等，则计算当前装配件的采购费，并计算当前装配件子项的物料的采购费。
												String newcgf=json.getString("CGF");
												BigDecimal cgfNew=new BigDecimal(newcgf);
												String CGF=rowMap.getString("CGF");//采购费											
													
													zcgfNew=sl.multiply(cgfNew);
													cgfnum=cgfnum.add(sl.multiply(cgfNew));
													String upSql= "update BO_EU_CPQ_COMPONENT_PART_GRID set DJ= '"+newdj+"',CLF= '"+newclf+"' ,RGF='"+newrgf+"',WXWG='"+newwx+"'"
													 		+ ",CGF='"+newcgf+"',ZJ='"+zjNew+"',ZCLF='"+zclfNew+"',ZGSF='"+zgsfNew+"',ZWX='"+zwxNew+"',ZCGF='"+zcgfNew+"' "
													 				+ " where  ID='"+id+"' ; ";
														//upSql=String.format(upSql, cldjNew,clfNew,zjNew,zclfNew,ID);
														sqlList.add(upSql);
														if(key.equals("RJC020002")) {
															System.out.println("bm:"+BM+",DJ:"+djnum+"ZJ:"+zjNew);
														}
													//将子项的编号存放在bhs中
														if(bhs.isEmpty()) {
															bhs=BM;
														}else {
															bhs=bhs+","+BM;
														}
										}
									}
							 }
								//更新装配件数据。
							 if(isTrue.equals("1")) {
								// DBSql.update(zpjsql, new Object[] {djnum,clfnum,rgfnum,wxfnum,cgfnum,zlnum,tjnum,key});
								 JSONObject json=new  JSONObject();
				    				json.put("DJ",djnum);	
				    				json.put("ZZFBL",data.getString("ZZFBL"));
				    				json.put("CLF",clfnum );			    				
				    				json.put("RGF",rgfnum);
				    				json.put("WXWG",wxfnum);
				    				json.put("CGF",cgfnum);				    	
				    				json.put("BH",BH);				    			
				    				json.put("ZJS", bhs);
				    				//存放在
				    				//userMap.put(key, json);
				    				 gxmap.put(key, json);
				    				 onemap.put(key, json);
				    				 if(key.equals("RJC020002")) {
											System.out.println("BH:"+key+",DJ:"+djnum);
											System.out.println("BH:"+key+",json:"+json.toString());
										}	
				    				 list.add(BH);
							 }	 
							 
							 
					 }
					// 如果获取的装配件的数量大于之前的数量，说明有有新增更新的装配件数据，如果没有则已经更新完毕,跳出循环。
					
						if (list.size()==i) {
							is = false;
						} 
				}
				//将获取到的装配件进行更新
				List<String> sqlLists=new ArrayList<>();
				if(gxmap.size()>0) {
					for (String key : gxmap.keySet()) {
						/**
						 * DJ=?,LSJ=?,BGLSJ=?,RGLSJ=?,JGLSJ=?,QTJN=?,QTJGT=?,RLJ=?,"
						+ "CLF=?,LSJCLF=?,BGLSJCLF=?,RGLSJCLF=?,JGLSJCLF=?,QTNCLF=?,QTTCLF=?,RKJCLF=?"
						+ "RGF=?,WXWG=?,CGF=?,ZL=?,TJ=? where BM=?";
						 */
						/**
						 * 			String zpjsql = "update BO_EU_CPQ_COMPONENT_PART set DJ=?,"
						+ "CLF=?,"
						+ "RGF=?,WXWG=?,CGF=?,ZL=?,TJ=? where BM=?";
						 */
						
						JSONObject json=gxmap.get(key);
						if(key.equals("RJC020002")) {
							System.out.println("json+++"+json.toString());
						}
						
						String dj=json.getString("DJ");
						String clf=json.getString("CLF");
						String rgf=json.getString("RGF");
						String bl=json.getString("ZZFBL").isEmpty()?"0":json.getString("ZZFBL");
						BigDecimal djnum = new BigDecimal(dj);
						BigDecimal clnum = new BigDecimal(clf);
						BigDecimal rgnum = new BigDecimal(rgf);
						BigDecimal blnum = new BigDecimal(bl);
						BigDecimal num = new BigDecimal("100");
						BigDecimal num1 = new BigDecimal("1");
						BigDecimal zzf=	clnum.add(rgnum).divide(num1.subtract(blnum.divide(num)), 4).multiply(blnum.divide(num));				
						//System.out.println("更新的装配件信息："+json);										
						//DBSql.update(zpjsql,new Object[] {json.getString("DJ"),json.getString("CLF"),json.getString("RGF"),json.getString("WXWG"),json.getString("CGF"),json.getString("ZL"),json.getString("TJ"),zzf,zzf.add(djnum),json.getString("BH")});
						 String upSql= "update BO_EU_CPQ_COMPONENT_PART set DJ= '"+json.getString("DJ")+"',CLF= '"+json.getString("CLF")+"' ,RGF='"+json.getString("RGF")+"',WXWG='"+json.getString("WXWG")+"',ZZF='"+zzf+"',DJZZF='"+zzf.add(djnum)+"' where BM='"+json.getString("BH")+"' ; ";
							//upSql=String.format(upSql, cldjNew,clfNew,zjNew,zclfNew,ID);
							sqlLists.add(upSql);
					}
				}
				
				if(sqlList.size()>0) {
					 String[] array3 = sqlList.toArray(new String[sqlList.size()]);	
					 DBSql.batch(array3);
				}
				if(sqlLists.size()>0) {
					String[] array2 = sqlLists.toArray(new String[sqlLists.size()]);					
					DBSql.batch(array2);  
				}	
			}
			
		} catch (Exception e) {
			throw e;
		}
		
	}

}
